package own.jdq.dic.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import own.jdq.dic.entity.*;
import own.jdq.dic.mapper.TDicAdrgListMapper;
import own.jdq.dic.mapper.TDicMdcMapper;
import own.jdq.dic.service.*;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jdq
 * @since 2021-11-13
 */
@Service
public class TDicAdrgListServiceImpl extends ServiceImpl<TDicAdrgListMapper, TDicAdrgList> implements TDicAdrgListService {
    @Autowired
    public TDicMdcService MdcService;
    @Autowired
    public TDicAdrgService adrgService;
    @Autowired
    public TDicAdrgListService adrgListService;
    @Autowired
    public TDicMccService mccService;
    @Autowired
    public TDicCcService ccService;
    @Autowired
    public TDicRemoveGroupService removeGroupService;
    @Autowired
    public TDicRemoveListService removeListService;

    @Override
    public Map<String, String> inbedGruop(Map<String, Object> psnMedicalInfo) {
        Map<String, Object> dicInfo = getDicInfo();
        InbedDrg inbedDrg=new InbedDrg();
        InbedDrg mdcdate=getMdcCode(dicInfo,psnMedicalInfo,inbedDrg);
        return null;
    }

    private InbedDrg getMdcCode(Map<String, Object> dicInfo, Map<String, Object> psnMedicalInfo, InbedDrg inbedDrg) {
        List<TDicAdrgList> mdrg = (List<TDicAdrgList>)dicInfo.get("mdrg");
        List<TDicAdrgList> mcode = (List<TDicAdrgList>)dicInfo.get("mcode");
        List<TDicAdrgList> moper = (List<TDicAdrgList>)dicInfo.get("moper");
        InbedDrg pregroup=preGroup(psnMedicalInfo,mdrg,mcode,moper,inbedDrg);
        return inbedDrg;
    }

    private InbedDrg preGroup(Map<String, Object> psnMedicalInfo, List<TDicAdrgList> mdrg, List<TDicAdrgList> mcode, List<TDicAdrgList> moper, InbedDrg inbedDrg) {

        String machinuserday=psnMedicalInfo.get("machineUseDay").toString();
        String machineUseHour=psnMedicalInfo.get("machineUseHour").toString();
       int day= machinuserday==""?0:Integer.parseInt(machinuserday);
        int hour= machineUseHour==""?0:Integer.parseInt(machineUseHour);
        LinkedList<TDicAdrgList> operList=new LinkedList<>();
        if ((day*24+hour)>96){
            for (TDicAdrgList oper:moper
                 ) {
              if(oper.getMdcId()==1){
                  operList.add(oper);
              }
            }
          List<String> medicalOperList=(List<String> )psnMedicalInfo.get("operList");
            for (String medicalOper:medicalOperList
                 ) {
                for (TDicAdrgList dicoper:operList
                     ) {
                    if (dicoper.getParamCode().equals(medicalOper)){
                        inbedDrg.setMdcId(1);
                        inbedDrg.setGroupStatus("已入组");
                        inbedDrg.setGroupCode1("A");
                    }
                }
            }
            return inbedDrg;
        }

        LinkedList<TDicAdrgList> newMdData=new LinkedList<>();
        for (TDicAdrgList adrgList: mdrg
             ) {
            if (adrgList.getMdcId()==25)
                newMdData.add(adrgList);
        }
        LinkedList<TDicAdrgList> newMCodeData=new LinkedList<>();
        for (TDicAdrgList adrgList: mcode
        ) {
            if (adrgList.getMdcId()==25)
                newMCodeData.add(adrgList);
        }
        List<String> medDiagList=(List<String>)psnMedicalInfo.get("diagList");

      return   inbedDrg;
    }

    public Map<String,Object> getDicInfo(){
       List<TDicMdc> tDicMdcList= MdcService.selectList(null);
       List<TDicAdrg> tDicAdrg=adrgService.selectList(null);
       List<TDicMcc> tDicMcc=mccService.selectList(null);
       List<TDicCc> tDicCc=ccService.selectList(null);
       List<TDicRemoveGroup> removeGroup=removeGroupService.selectList(null);
       List<TDicRemoveList> tDicRemoveList = removeListService.selectList(null);
        List<TDicAdrgList> mdragList=adrgListService.selectList(
               new EntityWrapper<TDicAdrgList>().eq("ADRG_LIST_TYPE","主要诊断"));
       List<TDicAdrgList> mcodeList=adrgListService.selectList(
               new EntityWrapper<TDicAdrgList>().eq("ADRG_LIST_TYPE","主诊编码"));
        List<TDicAdrgList> moperList=adrgListService.selectList(
                new EntityWrapper<TDicAdrgList>().eq("ADRG_LIST_TYPE","主要手术或操作"));
       Map<String,Object> dicMap=new HashMap<>();
       dicMap.put("mdc",tDicMdcList);
       dicMap.put("adrg",tDicAdrg);
       dicMap.put("mcc",tDicMcc);
       dicMap.put("cc",tDicCc);
       dicMap.put("remove",removeGroup);
       dicMap.put("removeList",tDicRemoveList);
       dicMap.put("mdrg",mdragList);
       dicMap.put("mcode",mcodeList);
       dicMap.put("moper",moperList);
       return dicMap;
    }
}
